from others import sup


def is_happy(n: int) -> bool:
    """快慢指针捕捉循环"""
    if n <= 0:
        return False
    slow, fast = n, sup(n)
    while slow != fast:
        slow = sup(slow)
        fast = sup(sup(fast))
    return slow == 1


def is_happy2(n: int) -> bool:
    """集合捕捉循环"""
    if n <= 0:
        return False
    cycle = set()
    while n not in cycle:
        if n == 1:
            return True
        cycle.add(n)
        n = sup(n)
    return False
